<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/wang-cheng/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/wang-cheng/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/wang-cheng/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/wang-cheng/images/logo.svg" color="#222">

<link rel="stylesheet" href="/wang-cheng/css/main.css">


<link rel="stylesheet" href="/wang-cheng/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"cheng-wang123.gitee.io","root":"/wang-cheng/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="锁的本质 用于保障数据的一致性  可以是原子性保障，也可以是使用补偿自旋锁   https:&#x2F;&#x2F;blog.csdn.net&#x2F;lioncatch&#x2F;article&#x2F;details&#x2F;105919666 偏向锁偏向第一个拿到锁的线程。 即第一个拿到锁的线程，锁会在对象头 Mark Word 中通过 CAS 记录该线程 ID，该线程以后每次拿锁时都不需要进行 CAS（指轻量级锁）。 如果该线程正在执行同步代">
<meta property="og:type" content="article">
<meta property="og:title" content="synchronized等锁机制">
<meta property="og:url" content="https://cheng-wang123.gitee.io/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/index.html">
<meta property="og:site_name" content="诚の博客">
<meta property="og:description" content="锁的本质 用于保障数据的一致性  可以是原子性保障，也可以是使用补偿自旋锁   https:&#x2F;&#x2F;blog.csdn.net&#x2F;lioncatch&#x2F;article&#x2F;details&#x2F;105919666 偏向锁偏向第一个拿到锁的线程。 即第一个拿到锁的线程，锁会在对象头 Mark Word 中通过 CAS 记录该线程 ID，该线程以后每次拿锁时都不需要进行 CAS（指轻量级锁）。 如果该线程正在执行同步代">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/%E9%87%8D%E9%87%8F%E7%BA%A7%E9%94%81.jpg">
<meta property="og:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/%E9%94%81%E5%8D%87%E7%BA%A7%E6%B5%81%E7%A8%8B.png">
<meta property="og:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/AQS.png">
<meta property="og:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/%E9%9D%9E%E5%85%AC%E5%B9%B3%E9%94%81.png">
<meta property="article:published_time" content="2022-05-04T16:27:41.000Z">
<meta property="article:modified_time" content="2022-05-15T02:52:50.402Z">
<meta property="article:author" content="王诚">
<meta property="article:tag" content="面试题">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cheng-wang123.gitee.io/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/%E9%87%8D%E9%87%8F%E7%BA%A7%E9%94%81.jpg">

<link rel="canonical" href="https://cheng-wang123.gitee.io/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>synchronized等锁机制 | 诚の博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/wang-cheng/atom.xml" title="诚の博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/wang-cheng/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">诚の博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记录学习中的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/wang-cheng/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/wang-cheng/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/wang-cheng/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/wang-cheng/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://cheng-wang123.gitee.io/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/wang-cheng/images/avatar.gif">
      <meta itemprop="name" content="王诚">
      <meta itemprop="description" content="命运对勇士低语，你无法抵御风暴；勇士低声回应，我就是风暴">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="诚の博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          synchronized等锁机制
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-05-05 00:27:41" itemprop="dateCreated datePublished" datetime="2022-05-05T00:27:41+08:00">2022-05-05</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-05-15 10:52:50" itemprop="dateModified" datetime="2022-05-15T10:52:50+08:00">2022-05-15</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="锁的本质"><a href="#锁的本质" class="headerlink" title="锁的本质"></a><strong>锁的本质</strong></h2><ul>
<li><p>用于保障数据的一致性</p>
</li>
<li><p>可以是原子性保障，也可以是使用补偿自旋锁</p>
</li>
</ul>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/lioncatch/article/details/105919666">https://blog.csdn.net/lioncatch/article/details/105919666</a></p>
<h2 id="偏向锁"><a href="#偏向锁" class="headerlink" title="偏向锁"></a><strong>偏向锁</strong></h2><p>偏向第一个拿到锁的线程。</p>
<p>即第一个拿到锁的线程，锁会在对象头 Mark Word 中通过 CAS 记录该线程 ID，该线程以后每次拿锁时都不需要进行 CAS（指轻量级锁）。</p>
<p>如果该线程正在执行同步代码块时有其他线程在竞争（指其他线程尝试 CAS 让 Mark Word 设置自己的线程 ID），会被升级为轻量级锁。</p>
<p>如果其他线程发现 Mark Word 里记的不是自己，且发现原持有偏向锁的线程已经执行完同步代码块，会尝试 CAS 把 Mark Word 中的改为自己的线程 ID。</p>
<h2 id="轻量级锁"><a href="#轻量级锁" class="headerlink" title="轻量级锁"></a><strong>轻量级锁</strong></h2><p>轻量级锁就是通过 CAS 进行加锁的。</p>
<p>JVM 会给线程的栈帧中创建一个叫锁记录 Lock Record 的空间，把对象头 Mark Word 复制到该空间里（Displaced Mark Word），并通过CAS 尝试把原对象头 Mark Word 中锁记录指针指向该锁记录。如果成功，表示线程拿到了锁。如果失败，则进行自旋（自旋锁），自旋超过一定次数时升级为重量级锁，这时该线程会被内核挂起。</p>
<h2 id="自旋锁"><a href="#自旋锁" class="headerlink" title="自旋锁"></a><strong>自旋锁</strong></h2><p>轻量级锁膨胀为重量级锁前，线程在执行 monitorenter 指令进入等待队列时，会通过自旋去尝试获得锁。</p>
<p>如果自旋超过一定次数时还未拿到锁，就会进入阻塞状态，等待内核来调度。此时会发生内核态与用户态之间的上下文切换，所以会影响性能（引入自旋锁就是为了减少这个开销）。</p>
<p>因为后面的线程也先进行自旋尝试获取锁，所以这对于已被阻塞的那些线程来说，会不公平。</p>
<h2 id="重量级锁"><a href="#重量级锁" class="headerlink" title="重量级锁"></a><strong>重量级锁</strong></h2><p>重量级锁就是通过内核来操作线程。因为频繁出现内核态与用户态的切换，会严重影响性能。</p>
<p>升级为重量级锁时会在堆中创建 monitor 对象，并将 Mark Word 指向该 monitor 对象。monitor 中有 cxq（ContentionList），EntryList ，WaitSet，owner：</p>
<img src="/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/%E9%87%8D%E9%87%8F%E7%BA%A7%E9%94%81.jpg" class title="重量级锁">
<h2 id="锁升级的流程图"><a href="#锁升级的流程图" class="headerlink" title="锁升级的流程图"></a><strong>锁升级的流程图</strong></h2><img src="/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/%E9%94%81%E5%8D%87%E7%BA%A7%E6%B5%81%E7%A8%8B.png" class title="锁升级流程">
<p><strong>升级流程</strong>：</p>
<ol>
<li><p>当线程第一次访问synchronized锁的时候，synchronized会由无锁状态变为偏向锁，偏向当前线程</p>
<ul>
<li>synchoronized锁对象会在对象头中的mark word中记录下当前线程的ID，然后将自己升级为偏向锁。</li>
<li>这个过程是由JVM去实现的</li>
</ul>
</li>
<li><p>当线程第二次访问synchronized锁的时候，发现synchronized锁对象是偏向自己的，那么自己不需要重新获取锁对象就可以直接执行里面的方法</p>
<ul>
<li>这样做可以提高线程的效率，不需要再转换线程上下文</li>
</ul>
</li>
<li><p>当另一个线程到来，想要获取这个锁对象。这时候synchronized锁就会升级为轻量级锁。</p>
<ul>
<li>此时这个线程就会进入自旋状态，不停地请求锁对象(此时会有性能消耗，因为cpu需要不停地进行线程的上下文切换)，看正在执行的线程是否已经释放掉了锁(该过程是在JVM中进行的)</li>
</ul>
</li>
<li><p>当有很多线程都来请求锁对象或者线程自旋了很多次，这时候锁对象就会升级称为重量级锁</p>
<ul>
<li>此时就由OS来完成。OS会将自旋的线程设置为等待状态，放入等待队列，让cpu先处理当前线程，等当前线程处理完了，等待队列当中的线程才会依次执行。</li>
</ul>
</li>
</ol>
<h2 id="ReentrantLock和synchronized对比"><a href="#ReentrantLock和synchronized对比" class="headerlink" title="ReentrantLock和synchronized对比"></a><strong>ReentrantLock和synchronized对比</strong></h2><ol>
<li><p>底层实现的区别</p>
<ul>
<li>synchronized是JVM层面的锁，是JAVA关键字，ReetrantLock是API层面的锁</li>
</ul>
</li>
<li><p>是否可手动释放</p>
<ul>
<li>synchronized不需要手动释放，系统会自动让线程释放对锁的占用。ReetrantLock则需要手动释放</li>
</ul>
</li>
<li><p>是否可中断</p>
<ul>
<li><p>synchronized是不可中断类型的锁，除非加锁的代码中出现异常或正常执行完成</p>
</li>
<li><p>ReetrantLock则可以中断</p>
</li>
</ul>
</li>
<li><p>是否为公平锁</p>
<ul>
<li>synchronized是非公平锁，ReentrantLock则既可以公平锁又可以非公平锁</li>
</ul>
</li>
<li><p>锁是否可绑定Condition</p>
<ul>
<li>synchronized 不能绑定。</li>
<li>ReentrantLock可以绑定</li>
</ul>
</li>
<li><p>锁的对象</p>
<ul>
<li><p>synchronized锁的是对象，锁是保存在对象头里面，根据对象头数据来标识是否有线程获得锁/争抢锁</p>
</li>
<li><p>ReentrantLock锁的是线程，根据进入的线程和int类型的state标识锁的获得/争抢</p>
</li>
</ul>
</li>
</ol>
<h2 id="ReentrantLock-的原理"><a href="#ReentrantLock-的原理" class="headerlink" title="ReentrantLock 的原理"></a><strong>ReentrantLock 的原理</strong></h2><p>ReentrantLock底层是由AQS去实现的</p>
<h3 id="AQS"><a href="#AQS" class="headerlink" title="AQS"></a><strong>AQS</strong></h3><p>AQS可以理解为一个可以实现锁的框架</p>
<p><strong>公平锁</strong><br><img src="/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/AQS.png" class title="AQS"></p>
<ul>
<li><p>第一步：获取状态的state的值</p>
<ol>
<li>如果state=0即代表锁没有被其他线程占用，执行第二步</li>
<li>如果state!=0则代表锁正在被其他线程占用，执行第三步</li>
</ol>
</li>
<li><p>第二步：判断队列中是否有线程在排队等待</p>
<ol>
<li>如果不存在则直接将锁的所有者设置为当前线程，且更新状态state</li>
<li>如果存在就入队</li>
</ol>
</li>
<li><p>第三步：判断锁的所有者是不是当前线程</p>
<ol>
<li>如果是则更新状态 state 的值</li>
<li>如果不是，线程进入队列排队等待</li>
</ol>
</li>
</ul>
<p><strong>非公平锁</strong><br><img src="/wang-cheng/2022/05/05/synchronized%E7%AD%89%E9%94%81%E6%9C%BA%E5%88%B6/%E9%9D%9E%E5%85%AC%E5%B9%B3%E9%94%81.png" class title="非公平锁"></p>
<ul>
<li><p>第一步：获取状态的 state 的值</p>
<ol>
<li>如果 state=0 即代表锁没有被其它线程占用，则设置当前锁的持有者为当前线程，该操作用 CAS 完成。</li>
<li>如果不为 0 或者设置失败，代表锁被占用进行下一步</li>
</ol>
</li>
<li><p>此时获取 state 的值</p>
<ol>
<li>如果是，则给 state+1，获取锁</li>
<li>如果不是，则进入队列等待</li>
<li>如果是 0，代表刚好线程释放了锁，此时将锁的持有者设为自己</li>
<li>如果不是 0，则查看线程持有者是不是自己</li>
</ol>
</li>
</ul>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a><strong>总结</strong></h2><p>ReentrantLock 和 Synchronized 的区别：</p>
<ul>
<li><p>其实 ReentrantLock 和 Synchronized 最核心的区别就在于 Synchronized 适合于并发竞争低的情况，因为 Synchronized 的锁升级如果最终升级为重量级锁在使用的过程中是没有办法消除的，意味着每次都要和 cpu 去请求锁资源，而 ReentrantLock 主要是提供了阻塞的能力，通过在高并发下线程的挂起，来减少竞争，提高并发能力。</p>
</li>
<li><p>Synchronized 是一个关键字，是由 JVM 层面去实现的，而 ReentrantLock 是由 java api 去实现的。</p>
</li>
<li><p>Synchronized 是隐式锁，可以自动释放锁，ReentrantLock 是显式锁，需要手动释放锁。</p>
</li>
<li><p>ReentrantLock 可以让等待锁的线程响应中断，而 Synchronized 却不行，使用 Synchronized 时，等待的线程会一直等待下去，不能够响应中断。</p>
</li>
<li><p>ReentrantLock 可以获取锁状态，而 Synchronized 不能。</p>
</li>
</ul>
<p><strong>动态高并发时为什么推荐 ReentrantLock 而不是 Synchronized？</strong></p>
<p>Synchronized 升级为重量级锁后无法在正常情况下完成降级，而 ReentrantLock 是通过阻塞来提高性能的，在设计模式上就体现出了对多线程情况的支持。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/wang-cheng/tags/%E9%9D%A2%E8%AF%95%E9%A2%98/" rel="tag"># 面试题</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/wang-cheng/2022/04/29/ConcurrentHashMap%E6%BA%90%E7%A0%81%E4%B8%93%E9%A2%98/" rel="prev" title="ConcurrentHashMap源码专题">
      <i class="fa fa-chevron-left"></i> ConcurrentHashMap源码专题
    </a></div>
      <div class="post-nav-item">
    <a href="/wang-cheng/2022/05/16/%E9%9D%A2%E7%BB%8F%E4%B8%80/" rel="next" title="面经一">
      面经一 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%94%81%E7%9A%84%E6%9C%AC%E8%B4%A8"><span class="nav-number">1.</span> <span class="nav-text">锁的本质</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%81%8F%E5%90%91%E9%94%81"><span class="nav-number">2.</span> <span class="nav-text">偏向锁</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BD%BB%E9%87%8F%E7%BA%A7%E9%94%81"><span class="nav-number">3.</span> <span class="nav-text">轻量级锁</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E6%97%8B%E9%94%81"><span class="nav-number">4.</span> <span class="nav-text">自旋锁</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%87%8D%E9%87%8F%E7%BA%A7%E9%94%81"><span class="nav-number">5.</span> <span class="nav-text">重量级锁</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%94%81%E5%8D%87%E7%BA%A7%E7%9A%84%E6%B5%81%E7%A8%8B%E5%9B%BE"><span class="nav-number">6.</span> <span class="nav-text">锁升级的流程图</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ReentrantLock%E5%92%8Csynchronized%E5%AF%B9%E6%AF%94"><span class="nav-number">7.</span> <span class="nav-text">ReentrantLock和synchronized对比</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ReentrantLock-%E7%9A%84%E5%8E%9F%E7%90%86"><span class="nav-number">8.</span> <span class="nav-text">ReentrantLock 的原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#AQS"><span class="nav-number">8.1.</span> <span class="nav-text">AQS</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">9.</span> <span class="nav-text">总结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">王诚</p>
  <div class="site-description" itemprop="description">命运对勇士低语，你无法抵御风暴；勇士低声回应，我就是风暴</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/wang-cheng/archives/">
        
          <span class="site-state-item-count">32</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/wang-cheng/tags/">
          
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王诚</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/wang-cheng/lib/anime.min.js"></script>
  <script src="/wang-cheng/lib/velocity/velocity.min.js"></script>
  <script src="/wang-cheng/lib/velocity/velocity.ui.min.js"></script>

<script src="/wang-cheng/js/utils.js"></script>

<script src="/wang-cheng/js/motion.js"></script>


<script src="/wang-cheng/js/schemes/pisces.js"></script>


<script src="/wang-cheng/js/next-boot.js"></script>




  















  

  

  

</body>
</html>
